//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-2015, Egret Technology Inc.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends eui.UILayer {
    /**
     * 加载进度界面
     * loading process interface
     */
    private loadingView: LoadingUI;
    protected createChildren(): void {
        super.createChildren();
        //inject the custom material parser
        //注入自定义的素材解析器
        var assetAdapter = new AssetAdapter();
        this.stage.registerImplementation("eui.IAssetAdapter",assetAdapter);
        this.stage.registerImplementation("eui.IThemeAdapter",new ThemeAdapter());
        //Config loading process interface
        //设置加载进度界面
        this.addChild(this.bg1);
        
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);
        
        //return;
        
        // initialize the Resource loading library
        //初始化Resource资源加载库
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
        
        this.startLoad3();
    }
    
    private bg1: eui.Image = new eui.Image;
    
    private startLoad3(): void
    {
        //创建 URLLoader 对象
        var loader: egret.URLLoader = new egret.URLLoader();
        //设置加载方式为纹理
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        //添加加载完成侦听
        loader.addEventListener(egret.Event.COMPLETE, this.onLoadComplete3, this);
        var url: string = "resource/assets/loading/1.png";
        var request: egret.URLRequest = new egret.URLRequest(url);
        //开始加载
        loader.load(request);

    }
    
    private onLoadComplete3(event: egret.Event): void
    {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        //获取加载到的纹理对象
        var texture: egret.Texture = <egret.Texture>loader.data;

        this.bg1.source = texture;
    }
    
    /**
     * 配置文件加载完成,开始预加载皮肤主题资源和preload资源组。
     * Loading of configuration file is complete, start to pre-load the theme configuration file and the preload resource group
     */
    private onConfigComplete(event:RES.ResourceEvent):void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        // load skin theme configuration file, you can manually modify the file. And replace the default skin.
        //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
        var theme = new eui.Theme("resource/default.thm.json", this.stage);
        theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);

        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("preload");
    }
    private isThemeLoadEnd: boolean = false;
    /**
     * 主题文件加载完成,开始预加载
     * Loading of theme configuration file is complete, start to pre-load the 
     */
    private onThemeLoadComplete(): void {
        this.isThemeLoadEnd = true;
        this.createScene();
    }
    private isResourceLoadEnd: boolean = false;
    /**
     * preload资源组加载完成
     * preload resource group is loaded
     */
    private onResourceLoadComplete(event:RES.ResourceEvent):void {
        if (event.groupName == "preload") {
            this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.isResourceLoadEnd = true;
            this.createScene();
        }
    }
    private createScene(){
        if(this.isThemeLoadEnd && this.isResourceLoadEnd){
            this.startCreateScene();
        }
    }
    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event:RES.ResourceEvent):void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
    private onResourceLoadError(event:RES.ResourceEvent):void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //ignore loading failed projects
        this.onResourceLoadComplete(event);
    }
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
    private onResourceProgress(event:RES.ResourceEvent):void {
        if (event.groupName == "preload") {
            this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }
    /**
     * 创建场景界面
     * Create scene interface
     */
    protected startCreateScene(): void {
        var jssdk = new JSSDK();
        jssdk.init();
        var layer:Page1 = new Page1(this);
        layer.start();
        this.addChild(layer);
    }

    private onButtonClick(e: egret.TouchEvent) {
        var layer: Page1 = new Page1(this);
        layer.start();
        this.addChild(layer);
        return;
        
        var panel = new eui.Panel();
        panel.title = "Title";
        panel.horizontalCenter = 0;
        panel.verticalCenter = 0;
        this.addChild(panel);
    }
    
    public hide(layer: egret.Sprite):void
    {
        layer.y = 0;
        egret.Tween.get(layer).to({
            y:-1008, ease: egret.Ease.bounceInOut
        }, 500).call(onComplete, this, [layer]);
        
        function onComplete(layer2: egret.Sprite): void
        {
            if(layer2.parent) layer2.parent.removeChild(layer2);
        }
    }
    
    public hide2(layer: egret.Sprite): void
    {
        layer.y = 0;
        egret.Tween.get(layer).to({
            y: 1008, ease: egret.Ease.bounceInOut
        }, 500).call(onComplete, this, [layer]);

        function onComplete(layer2: egret.Sprite): void
        {
            if(layer2.parent) layer2.parent.removeChild(layer2);
        }
    }
    
    public show(layer: egret.Sprite): void
    {
        this.addChild(layer);
        layer.y = -1008;
        egret.Tween.get(layer).to({
            y: 0, ease: egret.Ease.bounceInOut
        }, 500).call(onComplete, this, [layer]);

        function onComplete(layer2: egret.Sprite): void
        {
            layer2['start']();
        }
    
    }
    
    public toPage1(layer: egret.Sprite, isUp: Boolean): void {
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page1 = new Page1(this);
        this.show(p);
        return;
        
        this.removeChild(layer);
        var p: Page1 = new Page1(this);
        p.start();
        this.addChild(p);
    }
    

    public toPage2(layer: egret.Sprite, isUp: Boolean): void{
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page2 = new Page2(this);
        this.show(p);
        return;
        
        
        this.removeChild(layer);
        var p: Page2 = new Page2(this);
        p.start();
        this.addChild(p);
    }
    
    public toPage3(layer: egret.Sprite, isUp: Boolean): void{
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page3 = new Page3(this);
        this.show(p);
        return;
        
        this.removeChild(layer);
        var p: Page3 = new Page3(this);
        p.start();
        this.addChild(p);
    }
    
    public toPage4(layer: egret.Sprite, isUp: Boolean): void {
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page4 = new Page4(this);
        this.show(p);
        return;
        
        this.removeChild(layer);
        var p: Page4 = new Page4(this);
        p.start();
        this.addChild(p);
    }
    
    public toPage5(layer: egret.Sprite, isUp: Boolean): void {
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page5 = new Page5(this);
        this.show(p);
        return;
        
        this.removeChild(layer);
        var p: Page5 = new Page5(this);
        p.start();
        this.addChild(p);
    }
    
    public scale(parent:egret.Sprite, skin: string, w: number, h: number, time: number): egret.Sprite
    {
        var layer:egret.Sprite = new egret.Sprite;
        parent.addChild(layer);
        
        var sp: egret.Sprite = new egret.Sprite;
        layer.addChild(sp);

        var bg2: eui.Image = new eui.Image;
        bg2.source = skin;
        bg2.x = - (w >> 1);
        bg2.y = - (h>> 1);
        sp.x = w >> 1;
        sp.scaleX = sp.scaleY = 0.1;
        sp.addChild(bg2);
        
        egret.Tween.get(sp).to({
            scaleX: 1,scaleY: 1,ease: egret.Ease.bounceOut
        },time)
            
        return layer;
    }

    public toPage6(layer: egret.Sprite, isUp: Boolean): void{
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page6 = new Page6(this);
        this.show(p);
        return;
        
        this.removeChild(layer);
        var p: Page6 = new Page6(this);
        p.start();
        this.addChild(p);
    }
    
    public toPage7(layer: egret.Sprite, isUp: Boolean): void{
        isUp ? this.hide(layer) : this.hide2(layer);
        var p: Page7 = new Page7(this);
        this.show(p);
        return;
        this.removeChild(layer);
        var p: Page7 = new Page7(this);
        p.start();
        this.addChild(p);
    }
    
    public toPage8(): void{
        var p: Page8 = new Page8(this);
        p.start();
        this.addChild(p);
    }
    
    public Remove(layer: egret.Sprite): void {
        this.removeChild(layer);
    }
}
